package fr.orleans.index;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashSet;
import java.util.Set;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import entities.Administrator;
import entities.Match;
import entities.Player;
import entities.Referee;
import entities.Staff;
import entities.Team;
import entities.Tournament;
import enums.TournamentStatus;
import fr.orleans.tools.ManagedBeans;

@ManagedBean
@ViewScoped
public class Index extends ManagedBeans{

	private String login;
	private String password;

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String validate(){
		Administrator admin = this.getService().getWebAction().findAdministrator(getLogin());
		if(admin == null){
			FacesContext.getCurrentInstance().addMessage("validate", 
					new FacesMessage(FacesMessage.SEVERITY_ERROR, "errorLogin", "L'identifiant renseign� est inconnu. Veuillez r�essayer."));
			return "/modules/index/index.jsf";
		}else{
			if(admin.getPassword().equals(getPassword())){
				return "/modules/tournament/adminList.jsf";
			}
			else{
				FacesContext.getCurrentInstance().addMessage("validate", 
						new FacesMessage(FacesMessage.SEVERITY_ERROR, "errorPassword", "Le mot de passe renseign� ne correspond pas � l'identifiant. Veuillez r�essayer."));
				return "/modules/index/index.jsf";
			}
		}
	}

	@SuppressWarnings("unused")
	public String fillDb(){

		Administrator admin = new Administrator();
		admin.setLogin("admin");
		admin.setPassword("admin");
		this.getService().getWebAction().createAdministrator(admin);

		Tournament tournament = new Tournament();
		tournament.setListReferees(new HashSet<Referee>());
		tournament.setListTeams(new HashSet<Team>());
		tournament.setListMatchs(new HashSet<Match>());
		tournament.setName("Champions League");
		tournament.setPauseDuration(15);
		tournament.setHalfTimeDuration(45);
		tournament.setPlace("Lisbonne");
		tournament.setProlongationDuration(30);
		try {
			tournament.setStartDate(new SimpleDateFormat("yyyy-MM-dd").parse("2014-09-01"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		tournament.setStatus(TournamentStatus.preparation);

		Team team1 = new Team();
		team1.setName("Paris Saint Germain");
		team1.setListPlayers(new HashSet<Player>());
		team1.setListStaffMembers(new HashSet<Staff>());
		team1.setTournament(tournament);
		addStaff(team1, "Laurent", "Blanc", "Entraineur");
		addPlayer(team1, "Salvatore", "Sirigu", 30, "Gardien");
		addPlayer(team1, "Alex", "Costa", 13, "Defenseur");
		addPlayer(team1, "Thiago", "Silva", 2, "Defenseur");
		addPlayer(team1, "Gregory", "Van der Wiel", 23, "Defenseur");
		addPlayer(team1, "Maxwell", "Maxwell", 17, "Defenseur");
		addPlayer(team1, "Blaise", "Matuidi", 14, "Milieu");
		addPlayer(team1, "Thiago", "Motta", 8, "Milieu");
		addPlayer(team1, "Marco", "Verratti", 24, "Milieu");
		addPlayer(team1, "Javier", "Pastore", 27, "Milieu");
		addPlayer(team1, "Edinson", "Cavani", 9, "Attaquant");
		addPlayer(team1, "Zlatan", "Ibrahimovic", 10, "Attaquant");

		Team team2 = new Team();
		team2.setName("Borussia Dortmund");
		team2.setListPlayers(new HashSet<Player>());
		team2.setListStaffMembers(new HashSet<Staff>());
		team2.setTournament(tournament);
		addStaff(team2, "Jurgen", "Klopp", "Entraineur");
		addPlayer(team2, "Roman", "Weidenfeller", 1, "Gardien");
		addPlayer(team2, "Mats", "Hummels", 13, "Defenseur");
		addPlayer(team2, "Neven", "Subotic", 2, "Defenseur");
		addPlayer(team2, "Lukasz", "Piszceck", 23, "Defenseur");
		addPlayer(team2, "Marcel", "Schmelzer", 17, "Defenseur");
		addPlayer(team2, "Jakub", "Blaszczykowski", 14, "Milieu");
		addPlayer(team2, "Ilkay", "Gundogan", 8, "Milieu");
		addPlayer(team2, "Nuri", "Sahin", 24, "Milieu");
		addPlayer(team2, "Henrick", "MHkitaryan", 27, "Milieu");
		addPlayer(team2, "Marco", "Reus", 9, "Attaquant");
		addPlayer(team2, "Robert", "Lewandowski", 10, "Attaquant");

		Team team3 = new Team();
		team3.setName("FC Barcelona");
		team3.setListPlayers(new HashSet<Player>());
		team3.setListStaffMembers(new HashSet<Staff>());
		team3.setTournament(tournament);
		addStaff(team3, "Gerardo", "Martino", "Entraineur");
		addPlayer(team3, "Victor", "Valdes", 1, "Gardien");
		addPlayer(team3, "Jordi", "Alba", 2, "Defenseur");
		addPlayer(team3, "Daniel", "Alves", 3, "Defenseur");
		addPlayer(team3, "Gerard", "Pique", 4, "Defenseur");
		addPlayer(team3, "Carles", "Puyol", 5, "Defenseur");
		addPlayer(team3, "Sergio", "Busquets", 6, "Milieu");
		addPlayer(team3, "Francesc", "Fabregas", 7, "Milieu");
		addPlayer(team3, "Andres", "Iniesta", 8, "Milieu");
		addPlayer(team3, "Xavi", "Hernandez", 9, "Milieu");
		addPlayer(team3, "Neymar", "Neymar", 10, "Attaquant");
		addPlayer(team3, "Lionel", "Messi", 11, "Attaquant");

		Team team4 = new Team();
		team4.setName("FC Bayern Munchen");
		team4.setListPlayers(new HashSet<Player>());
		team4.setListStaffMembers(new HashSet<Staff>());
		team4.setTournament(tournament);
		addStaff(team4, "Josep", "Guardiola", "Entraineur");
		addPlayer(team4, "Manuel", "Neuer", 1, "Gardien");
		addPlayer(team4, "David", "Alaba", 2, "Defenseur");
		addPlayer(team4, "Jerome", "Boateng", 3, "Defenseur");
		addPlayer(team4, "Bonfim", "Dante", 4, "Defenseur");
		addPlayer(team4, "Phillip", "Lahm", 5, "Defenseur");
		addPlayer(team4, "Mario", "Gotze", 6, "Milieu");
		addPlayer(team4, "Franck", "Ribery", 7, "Milieu");
		addPlayer(team4, "Basian", "Schweinsteiger", 8, "Milieu");
		addPlayer(team4, "Thomas", "Muller", 9, "Milieu");
		addPlayer(team4, "Mario", "Mandzukic", 10, "Attaquant");
		addPlayer(team4, "Arjen", "Robben", 11, "Attaquant");

		Team team5 = new Team();
		team5.setName("Real Madrid");
		team5.setListPlayers(new HashSet<Player>());
		team5.setListStaffMembers(new HashSet<Staff>());
		team5.setTournament(tournament);
		addStaff(team5, "Carlo", "Ancelloti", "Entraineur");
		addPlayer(team5, "Iker", "Casillas", 1, "Gardien");
		addPlayer(team5, "Marcelo", "Marcelo", 2, "Defenseur");
		addPlayer(team5, "Pepe", "Pepe", 3, "Defenseur");
		addPlayer(team5, "Sergio", "Ramos", 4, "Defenseur");
		addPlayer(team5, "Raphael", "Varane", 5, "Defenseur");
		addPlayer(team5, "Xabi", "Alonso", 6, "Milieu");
		addPlayer(team5, "Gareth", "Bale", 7, "Milieu");
		addPlayer(team5, "Isco", "Isco", 8, "Milieu");
		addPlayer(team5, "Angel", "Di Maria", 9, "Milieu");
		addPlayer(team5, "Karim", "Benzema", 10, "Attaquant");
		addPlayer(team5, "Cristiano", "Ronaldo", 11, "Attaquant");

		Team team6 = new Team();
		team6.setName("AS Monaco");
		team6.setListPlayers(new HashSet<Player>());
		team6.setListStaffMembers(new HashSet<Staff>());
		team6.setTournament(tournament);
		addStaff(team6, "Claudio", "Ranieri", "Entraineur");
		addPlayer(team6, "Danijel", "Subasic", 1, "Gardien");
		addPlayer(team6, "Eric", "Abidal", 2, "Defenseur");
		addPlayer(team6, "Ricardo", "Carvalho", 3, "Defenseur");
		addPlayer(team6, "Fabinho", "Fabinho", 4, "Defenseur");
		addPlayer(team6, "Andrea", "Raggi", 5, "Defenseur");
		addPlayer(team6, "Geoffrey", "Kondogbia", 6, "Milieu");
		addPlayer(team6, "Joao", "Moutinho", 7, "Milieu");
		addPlayer(team6, "Mounir", "Obbadi", 8, "Milieu");
		addPlayer(team6, "James", "Rodriguez", 9, "Milieu");
		addPlayer(team6, "Radamel", "Falcao", 10, "Attaquant");
		addPlayer(team6, "Emmanuel", "Riviere", 11, "Attaquant");

		Team team7 = new Team();
		team7.setName("Juventus FC");
		team7.setListPlayers(new HashSet<Player>());
		team7.setListStaffMembers(new HashSet<Staff>());
		team7.setTournament(tournament);
		addStaff(team7, "Antonio", "Conte", "Entraineur");
		addPlayer(team7, "Gianluigi", "Buffon", 1, "Gardien");
		addPlayer(team7, "Leonardo", "Bonucci", 2, "Defenseur");
		addPlayer(team7, "Giorgio", "Chiellini", 3, "Defenseur");
		addPlayer(team7, "Paolo", "De Ceglie", 4, "Defenseur");
		addPlayer(team7, "Angelo", "Ogbonna", 5, "Defenseur");
		addPlayer(team7, "Arturo", "Vidal", 6, "Milieu");
		addPlayer(team7, "Andrea", "Pirlo", 7, "Milieu");
		addPlayer(team7, "Paul", "Pogba", 8, "Milieu");
		addPlayer(team7, "Claudio", "Marchisio", 9, "Milieu");
		addPlayer(team7, "Carlos", "Tevez", 10, "Attaquant");
		addPlayer(team7, "Fernando", "Llorente", 11, "Attaquant");


		Team team8 = new Team();
		team8.setName("Team 8");
		team8.setListPlayers(new HashSet<Player>());
		team8.setListStaffMembers(new HashSet<Staff>());
		team8.setTournament(tournament);

		Set<Team> listTeam = new HashSet<Team>();
		listTeam.add(team1);
		listTeam.add(team2);
		listTeam.add(team3);
		listTeam.add(team4);
		listTeam.add(team5);
		listTeam.add(team6);
		listTeam.add(team7);
		listTeam.add(team8);
		tournament.getListTeams().addAll(listTeam);

		Referee referee1 = addReferee("Clement", "Turpin", tournament);
		Referee referee2 = addReferee("Laurent", "Duhamel", tournament);
		Referee referee3 = addReferee("Tony", "Chapron", tournament);
		Referee referee4 = addReferee("Stephane", "Lannoy", tournament);

		/*
		Match match1 = new Match();
		match1.setHomeTeam(team1);
		match1.setOutsideTeam(team2);
		match1.setMatchNumber(1);
		match1.setRoundNumber(1);
		try {
			match1.setStartDate(new SimpleDateFormat("yyyy-MM-dd").parse("2014-09-03"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		match1.setStatus(MatchStatus.preparation);
		referee1.getListMatchs().add(match1);
		match1.setReferee(referee1);
		match1.setTournament(tournament);


		Match match2 = new Match();
		match2.setHomeTeam(team3);
		match2.setOutsideTeam(team4);
		match2.setMatchNumber(2);
		match2.setRoundNumber(1);
		try {
			match2.setStartDate(new SimpleDateFormat("yyyy-MM-dd").parse("2014-09-04"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		match2.setStatus(MatchStatus.preparation);
		referee1.getListMatchs().add(match2);
		match2.setReferee(referee2);
		match2.setTournament(tournament);


		Match match3 = new Match();
		match3.setHomeTeam(team5);
		match3.setOutsideTeam(team6);
		match3.setMatchNumber(3);
		match3.setRoundNumber(1);
		try {
			match3.setStartDate(new SimpleDateFormat("yyyy-MM-dd").parse("2014-09-03"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		match3.setStatus(MatchStatus.running);
		referee3.getListMatchs().add(match3);
		match3.setReferee(referee3);
		match3.setTournament(tournament);


		tournament.getListMatchs().add(match1);
		tournament.getListMatchs().add(match2);
		tournament.getListMatchs().add(match3);

		 */

		getService().getWebAction().createTournament(tournament);

		return null;
	}

	private void addPlayer(Team team, String firstName, String lastName, int number,
			String position) {
		Player player = new Player();
		player.setFirstName(firstName);
		player.setLastName(lastName);
		player.setNumber(number);
		player.setPosition(position);
		player.setTeam(team);
		team.getListPlayers().add(player);
	}

	private void addStaff(Team team, String firstName, String lastName, String typeStaff) {
		Staff staff = new Staff();
		staff.setFirstName(firstName);
		staff.setLastName(lastName);
		staff.setTypeStaff(typeStaff);
		staff.setTeam(team);
		team.getListStaffMembers().add(staff);
	}

	private Referee addReferee(String firstName, String lastName, Tournament tournament) {
		Referee referee = new Referee();
		referee.setListMatchs(new HashSet<Match>());
		referee.setFirstName(firstName);
		referee.setLastName(lastName);
		referee.setTournament(tournament);
		tournament.getListReferees().add(referee);
		return referee;
	}
}